package com.wf.util;


import cn.hutool.core.util.StrUtil;
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.VoidResult;
import com.wf.constant.RedisConstant;
import com.wf.properties.AliYunProperties;
import com.wf.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;

/**
 * 阿里云OSS工具类。
 *
 * @author mpk
 */
@Slf4j
@Component
@EnableConfigurationProperties(AliYunProperties.class)
public class AliOssUtil {

    private final AliYunProperties aliYunProperties;
    public AliOssUtil(AliYunProperties aliYunProperties) {
        this.aliYunProperties = aliYunProperties;
    }

    @Autowired
    RedisUtil redisUtil;

    /**
     * 文件上传
     *
     * @param bytes
     * @param objectName
     * @return
     */
    public String upload(byte[] bytes, String objectName) {

        OSS ossClient = null;
        try {
            ossClient = new OSSClientBuilder()
                    .build(aliYunProperties.getOss().getEndpoint(), aliYunProperties.getAccessKeyId(), aliYunProperties.getAccessKeySecret());
            // 创建PutObject请求。
            Long sSet=0L;
            if(StrUtil.endWith(objectName,".jpg")||StrUtil.endWith(objectName,".png")||StrUtil.endWith(objectName,".gif")||StrUtil.endWith(objectName,".jpeg"))
            {
                sSet = redisUtil.sSet(RedisConstant.APARTMENT_ALL_IMG,objectName);
            }else if(StrUtil.endWith(objectName,".pdf")||StrUtil.endWith(objectName,".doc")||StrUtil.endWith(objectName,".docx"))
            {
                sSet = redisUtil.sSet(RedisConstant.APARTMENT_ALL_FILE,objectName);
            }

            if(sSet!=0L)
            {
                ossClient.putObject(aliYunProperties.getOss().getBucketName(), objectName, new ByteArrayInputStream(bytes));
            }


        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }

        //文件访问路径规则 https://BucketName.Endpoint/ObjectName
        StringBuilder stringBuilder = new StringBuilder("https://");
        stringBuilder
                .append(aliYunProperties.getOss().getBucketName())
                .append(".")
                .append(aliYunProperties.getOss().getEndpoint())
                .append("/")
                .append(objectName);


        log.info("文件上传到:{}", stringBuilder);

        return stringBuilder.toString();
    }

    /**
     * 删除文件
     * @param objectName
     * @return
     */

    public String delete(String objectName) {
        OSS ossClient = null;
        ossClient = new OSSClientBuilder()
                .build(aliYunProperties.getOss().getEndpoint(), aliYunProperties.getAccessKeyId(), aliYunProperties.getAccessKeySecret());
        // 创建PutObject请求。
        try {
            // 删除文件。
            VoidResult result = ossClient.deleteObject(aliYunProperties.getOss().getBucketName(), objectName);

        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }

        return "删除成功";

    }
}
